package com.too.mq.producer;

import com.too.mq.config.RedisStreamConfig;
import com.too.mq.util.RedisStreamUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息生产者
 *
 * @author ouyf
 * @date 2025/7/18 10:53
 * <p>
 * Copyright (c) Siemens Mobility TBDS 2025, All Rights Reserved, Confidential
 */
@Slf4j
@Component
public class MessageProducer {

    @Autowired
    RedisStreamUtil redisStreamUtil;

    // 生产消息的方法
    @Scheduled(initialDelay = 5000, fixedRate = 5000) // 每5秒生产一次消息
    public void produceMessage() {
        // 这里可以实现将消息发送到消息队列的逻辑
        // 例如，使用 Redis Stream、Kafka 等消息中间件
        Map<String, Object> messageMap = new HashMap<>();
        messageMap.put("message", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        redisStreamUtil.addMap(RedisStreamConfig.streamName, messageMap);
        log.info("Producing message: {}", messageMap);
    }
}
